package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;


    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(queryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void updateCate(CategoryDTO categoryDTO) {
        log.info("当前员工id=============================：{}", BaseContext.getCurrentId());
        Category category = Category.builder()
//                .updateTime(LocalDateTime.now())
                .id(categoryDTO.getId())
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                .type(categoryDTO.getType())
                .updateUser(BaseContext.getCurrentId())
                .build();
        if (categoryDTO.getId() == null)   {
//            category.setCreateTime(LocalDateTime.now());
//            category.setCreateUser(BaseContext.getCurrentId());
            category.setStatus(StatusConstant.DISABLE);
            categoryMapper.add(category);
        } else {
            categoryMapper.update(category);
        }

    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Category category = Category.builder().status(status)
                .id(id)
                .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .build();
        categoryMapper.update(category);
    }

    @Override
    public void delCate(Long id) {
        // 查询分类是否关联菜品
        Integer i = dishMapper.countByCategoryId(id);
        if (i > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        Integer j = setmealMapper.countByCategoryId(id);
        if (j > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        // 此时调用
        categoryMapper.delCate(id);
    }

    @Override
    public ArrayList<CategoryDTO> list(Integer type) {
        return categoryMapper.list(type);
    }
}
